package com.example.panda;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.view.MenuItem;
import com.example.panda.adapter.MyAdapter;
import com.example.panda.fragment.HomeFragment;
import com.example.panda.fragment.MeFragment;
import com.example.panda.fragment.SquareFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity{
    //底部导航
    ViewPager viewPager;
    MyAdapter myAdapter;
    BottomNavigationView bottomNavigationView;
    List<Fragment> fragmentList = new ArrayList<>();

    //    private Spinner homeSpinner;
//    private List<String> home_spinner_list;
//    private ArrayAdapter<String> home_spinner_adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        homeSpinner = (Spinner)homeView. findViewById(R.id.spinner);
//        //数据
//        home_spinner_list = new ArrayList<String>();
//        home_spinner_list.add("北京");
//        home_spinner_list.add("上海");
//        home_spinner_list.add("广州");
//        home_spinner_list.add("深圳");
//        home_spinner_list.add("成都");
//        home_spinner_list.add("德阳");
//        home_spinner_list.add("绵阳");
//        home_spinner_list.add("贵州");
//        //适配器
//        home_spinner_adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, home_spinner_list);
//        //设置样式
//        home_spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        //加载适配器
//        homeSpinner.setAdapter(home_spinner_adapter);
        getFragment();
        initView();

    }

    //底部导航栏部分
    private void getFragment() {
        Fragment homeFragment = new HomeFragment();
        fragmentList.add(homeFragment);
        Fragment squareFragment = new SquareFragment();
        fragmentList.add(squareFragment);
        Fragment meFragment = new MeFragment();
        fragmentList.add(meFragment);
    }
    private void initView(){
        bottomNavigationView = findViewById(R.id.viewpager_bottomNav);
        bottomNavigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
        viewPager = findViewById(R.id.viewpager_viewpager);
        myAdapter = new MyAdapter(getSupportFragmentManager(),0,fragmentList);
        viewPager.setAdapter(myAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.nv_home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.nv_square:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.nv_me:
                        viewPager.setCurrentItem(2);
                        break;
                }
                return true;
            }
        });
    }
}
